//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include <stdio.h>
#include "StockUnit.h"
#include "translateSina.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
StockRecordString stockRecord_m[100];
//---------------------------------------------------------------------------
const int len_c = 1024;
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{

  char stockInitString[len_c];
  int num;

  FILE *fp;

  if ((fp = fopen("stock.ini","r")) != NULL)
  {
    fgets(stockInitString, sizeof(stockInitString)-1, fp);
    stockInitString[len_c-1]=0;
    fclose(fp);
  }
  else
  {
  strcpy(stockInitString, "sz159901,sz159902,sz160706,sh510880,sh510050,sh500002,\
sz184698,sh500009,sz184688,sz160505,sz184692,sz000063,sh510180,sh600866,\
sz184721,sz000039,sz000717,sz031006,sz000792,sz000060");
  }
  StockEdit->Text = stockInitString;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
    AnsiString stockString;
    char *char_in;
    int k = 0;
    static int i=0;
    int numOfRecords = 0;
    i++;
    stockString = IdHTTP1->Get("/rn=1210168194"+IntToStr(i)+"&list=" + StockEdit->Text);
    char_in = stockString.c_str();
    numOfRecords = translateStock(char_in, stockRecord_m);
    // AnsiString stockStringTmp = stockRecord_m.close;
    PriceStaticText->Caption = "";
    for (k=0; k<numOfRecords; k++)
    {
        PriceStaticText->Caption = PriceStaticText->Caption + stockRecord_m[k].close;
        PriceStaticText->Caption = PriceStaticText->Caption + " ";
    }
    if (numOfRecords != 0)
    {
        FILE *fp;
        if ((fp = fopen("stock.txt","wt")) != NULL)
        {
            fputs(PriceStaticText->Caption.c_str(),fp);
            fclose(fp);
        }
        else
        {
            ShowMessage("Create File Error!");
        }
    }
    PriceStaticText->Caption = "";
    for (k=0; k<numOfRecords; k++)
    {
        PriceStaticText->Caption = PriceStaticText->Caption + stockRecord_m[k].lastClose;
        PriceStaticText->Caption = PriceStaticText->Caption + " ";
    }
    if (numOfRecords != 0)
    {
        FILE *fp;
        if ((fp = fopen("stockPre.txt","wt")) != NULL)
        {
            fputs(PriceStaticText->Caption.c_str(),fp);
            fclose(fp);
        }
        else
        {
            ShowMessage("Create File Error!");
        }
    }
}
//---------------------------------------------------------------------------

